<template>
    <div class="column-detail-page w-75 mx-auto">
      <div class="row mb-4 border-bottom pb-4 align-items-center" v-if="column">
        <div class="col-3 text-center">
          <img :src="column.avatar.url" />
        </div>
        <div class="col-9">
          <h1>{{ column.title }}</h1>
          <p class="text-muted">{{ column.description }}</p>
        </div>
      </div>
      <post-list :list="posts"></post-list>
    </div>
</template>

<script lang="ts">
import { computed, defineComponent, onMounted } from 'vue'
import { useRoute } from 'vue-router'

import { useStore } from 'vuex'
import PostList from '../components/PostList.vue'

export default defineComponent({
  components: {
    PostList
  },
  setup () {
    const route = useRoute()
    const store = useStore()
    const currentId = route.params.id
    onMounted(() => {
      /* 防止文章头部消失 */
      store.dispatch('fetchColumn', currentId)
      store.dispatch('fetchPosts', currentId)
    })
    // const column = computed(() => {
    //   return store.getters.getColumnById(currentId)
    // })
    const column = computed(() => {
      const selectColumn = store.getters.getColumnById(currentId)
      return selectColumn
    })
    const posts = computed(() => {
      return store.getters.getPostsByCid(currentId)
    })
    return {
      column,
      posts
    }
  }
})
</script>
